package com.v9.common.dto;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.util.List;

/**
 * 树控件数据
 * @author lunshi
 */
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class TreeNodeDto {
    @Schema(description = "id")
    private String id;
    @Schema(description = "value")
    private String value;
    @Schema(description = "标签")
    private String label;
    @Schema(description = "类型")
    private Integer type;
    @Schema(description = "禁用选择")
    private Boolean isDisabled;
    @Schema(description = "排序")
    private Integer seq;
    @Schema(description = "数据")
    private Object data;
    @Schema(description = "子数据")
    private List<TreeNodeDto> children;

    @JsonIgnore
    private TreeNodeDto parent;

    public String getValue(){
        return id;
    }


    public static TreeNodeDto find(TreeNodeDto node,String id){
        if(node.id.equals(id)){
            return node;
        }
        List<TreeNodeDto> list = node.getChildren();
        if(list !=null){
            for(TreeNodeDto item:list){
                if(item.getId().equals(id)){
                    return item;
                }
                return find(item,id);
            }
        }
        return null;
    }

    public static TreeNodeDto findChild(TreeNodeDto node,String id){
        List<TreeNodeDto> list = node.getChildren();
        if(list !=null){
            for(TreeNodeDto item:list){
                if(item.getId().equals(id)){
                    return item;
                }
                return findChild(item,id);
            }
        }
        return null;
    }

    @Override
    public String toString() {
      return null;
    }
}
